package com.happy3w.cmds.maven.model;

import lombok.Data;

import java.nio.file.Path;

@Data
public class ClassInfo {
    private String className;
    private String packageName;
    private ModuleFile moduleFile;

    public String fullClassName() {
        return packageName + "." + className;
    }

    public static ClassInfo from(Path javaFilePath, String modulePath, ModuleFile moduleFile) {
        String fileName = javaFilePath.getFileName().toString();
        String fullPath = javaFilePath.toString();
        if (!fullPath.endsWith(".java")) {
            return null;
        }

        ClassInfo classInfo = new ClassInfo();
        classInfo.className = fileName.replace(".java", "");
        classInfo.packageName = fullPath
                .substring(modulePath.length() + 1, fullPath.length() - fileName.length() - 1)
                .replaceAll("\\\\", ".")
                .replaceAll("src\\.(main|test)\\.java\\.(.*)", "$2");
        classInfo.moduleFile = moduleFile;
        return classInfo;
    }
}
